<?php

class DBMEntity {
	/**
	 * The schema that contains this table
	 *
	 * @var string
	 */
	private $schema = null;
	
	/**
	 * The prefix of this to be used on the commands
	 *
	 * @var string
	 */
	private $alias = null;
	
	/**
	 * The name of the table in the database
	 *
	 * @var string
	 */
	private $table;

	/**
	 * The identification of this entity
	 *
	 * @var unknown_type
	 */
	private $name;
	
	/**
	 * The class of this entity
	 *
	 * @var Class
	 */
	private $class = null;
	
	/**
	 * Collection of the fields of this entity
	 *
	 * @var array
	 */
	private $attributes = array();

	/**
	 * The primary key of this entity
	 *
	 * @var DBMPrimaryKey
	 */
	private $primaryKey = null;
	
	/**
	 * Collection of the relationships of this entity
	 *
	 * @var unknown_type
	 */
	private $relationships = array();
	
	/** gets / sets */
	public function getSchema(){
		return $this->schema;
	}
	public function setSchema($schema){
		$this->schema = $schema;
	}
	
	public function getAlias(){
		if($this->alias == null){
			return $this->getTable();
		} else {
			return $this->alias;
		}
	}
	public function setAlias($alias){
		$this->alias = $alias;
	}
	
	public function getTable(){
		return $this->table;
	}
	public function setTable($table){
		$this->table = $table;
	}
	
	public function getName(){
		return $this->name;
	}
	public function setName($name){
		$this->name = $name;
	}
	
	public function getClass(){
		return $this->class;
	}
	public function setClass($class){
		$this->class = $class;
	}
	
	public function getAttributes(){
		return $this->attributes;
	}
	public function setAttributes($attributes){
		$this->attributes = $attributes;
	}
	
	public function getPrimaryKey(){
		return $this->primaryKey;
	}
	public function setPrimaryKey(DBMPrimaryKey $primaryKey){
		// get the attributes of the primary key
		$attributes = $primaryKey->getAttributes();
		
		// search the attributes
		foreach($attributes as &$attribute){
			$this->addAttribute($attribute);
		}
		
		// set the primary key
		$this->primaryKey = $primaryKey;
	}
	
	public function getRelationships(){
		return $this->relationships;
	}
	public function setRelationships($relationships){
		$this->relationships = $relationships;
	}
	
	/** business */
	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param string $table
	 * @param string $class
	 * @param string $schema
	 * @param string $alias
	 * @param array $attributes
	 */
	public function __construct($name, $table, $class = null, $schema = null, $alias = "", $attributes = array()){
		// set the name
		$this->setName($name);
		
		// set the database name
		$this->setTable($table);
		
		// set the schema
		$this->setSchema($schema);
		
		// set the alias
		$this->setAlias($alias);
		
		// set the class
		$this->setClass($class);
		
		// parse the fields
		if(!is_array($attributes) && $attributes instanceof DBMAttribute){
			$this->addAttribute($attributes);
		} elseif(is_array($attributes)){
			foreach($attributes as $attribute){
				$this->addAttribute($attribute);
			}
		}
	}
	
	/**
	 * Add an attribute to this entity
	 *
	 * @param DBMAttribute $attribute The attribute
	 */
	public function addAttribute(DBMAttribute $attribute){
		// set the entity on the attribute
		$attribute->setEntity($this);
		
		// register the attribute on this entity
		$this->attributes[$attribute->getName()] = $attribute;
	}
	
	public function addRelationships(DBMRelationship $relationship){
		// check if the attribute of the relationship is from this entity
		$this->relationships[$relationship->getAttribute()] = $relationship;	
	}
	
	public function getAttributeByName($name){
		// check if the specified attribute exists in this entity
		if(isset($this->attributes[$name]) && $this->attributes[$name] instanceof DBMAttribute){
			return $this->attributes[$name];
		} else {
			return null;
		}
	}

	/**
	 * Search for an attribute by its alias
	 *
	 * @param string $alias
	 * @return DBMAttribute The attribute or null
	 */
	public function getAttributeByString($alias){
		// get the attributes
		$attributes = $this->getAttributes();
		
		// search on the attributes for the specified alias
		foreach($attributes as $attribute){
			// get the alias and compare
			$attributeStr = $attribute->__toString();

			if($attributeStr == $alias || substr($attributeStr, 0, 30) == $alias){
				return $attribute;
			}
		}
		
		// not found, return null
		return null;
	}
	
	public function haveRelationshipWith(DBMEntity $entity){
		// get the relationships
		$relationships = $this->getRelationships();
		
		// search the relationships
		foreach($relationships as $relationship){
			// get the relationship entity
			$relationshipEntity = $relationship->getTargetEntity();
			
			// check if they match
			if($entity->getClass() == DBMEntityHolder::getInstance()->getEntity($relationshipEntity)->getClass()){
				return true;
			}
		}
		
		// no relationships found
		return false;
	}
	
	public function __toString(){
		return $this->getName();
	}
}

?>